<template>
  <p-drawer
    :title="title"
    :width="width"
    placement="right"
    :closable="false"
    @close="close"
    :visible="visible">
  
    <p-spin :spinning="confirmLoading">
      <p-form :form="form">

        <p-form-item label="物料编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'code', validatorRules.code]" placeholder="请输入物料编号"></p-input>
        </p-form-item>
        <p-form-item label="物料名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'name', validatorRules.name]" placeholder="请输入物料名称"></p-input>
        </p-form-item>
        <p-form-item label="重量范围" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'weightScope', validatorRules.weightScope]" placeholder="请输入重量范围"></p-input>
        </p-form-item>
        <p-form-item label="体积范围" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'volumeScope', validatorRules.volumeScope]" placeholder="请输入体积范围"></p-input>
        </p-form-item>
        <p-form-item label="外部物料组" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'extendGroup', validatorRules.extendGroup]" placeholder="请输入外部物料组"></p-input>
        </p-form-item>
        <p-form-item label="ABC标示" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'markAbc', validatorRules.markAbc]" placeholder="请输入ABC标示"></p-input>
        </p-form-item>
        <p-form-item label="旧的物料编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'codeOld', validatorRules.codeOld]" placeholder="请输入旧的物料编码"></p-input>
        </p-form-item>
        <p-form-item label="最小单元体" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'minimumElement', validatorRules.minimumElement]" placeholder="请输入最小单元体"></p-input>
        </p-form-item>
        <p-form-item label="批数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input-number v-decorator="[ 'batchNumber', validatorRules.batchNumber]" placeholder="请输入批数量" style="width: 100%"/>
        </p-form-item>
        <p-form-item label="最少收货数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input-number v-decorator="[ 'minimumReceived', validatorRules.minimumReceived]" placeholder="请输入最少收货数量" style="width: 100%"/>
        </p-form-item>
        <p-form-item label="超领接收允差" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input-number v-decorator="[ 'overlapedTolerance', validatorRules.overlapedTolerance]" placeholder="请输入超领接收允差" style="width: 100%"/>
        </p-form-item>
        <p-form-item label="最小批量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input-number v-decorator="[ 'minimumNumber', validatorRules.minimumNumber]" placeholder="请输入最小批量" style="width: 100%"/>
        </p-form-item>
        <p-form-item label="最大批量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input-number v-decorator="[ 'maxNumber', validatorRules.maxNumber]" placeholder="请输入最大批量" style="width: 100%"/>
        </p-form-item>
        <p-form-item label="最大层次" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'largestLevel', validatorRules.largestLevel]" placeholder="请输入最大层次"></p-input>
        </p-form-item>
        <p-form-item label="标准税价" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input-number v-decorator="[ 'taxStandard', validatorRules.taxStandard]" placeholder="请输入标准税价" style="width: 100%"/>
        </p-form-item>
        <p-form-item label="税价1" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input-number v-decorator="[ 'taxPrice1', validatorRules.taxPrice1]" placeholder="请输入税价1" style="width: 100%"/>
        </p-form-item>
        <p-form-item label="商业价格1" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input-number v-decorator="[ 'tradePrice1', validatorRules.tradePrice1]" placeholder="请输入商业价格1" style="width: 100%"/>
        </p-form-item>
        <p-form-item label="税价2" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input-number v-decorator="[ 'taxPrice2', validatorRules.taxPrice2]" placeholder="请输入税价2" style="width: 100%"/>
        </p-form-item>
        <p-form-item label="商业价格2" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input-number v-decorator="[ 'tradePrice2', validatorRules.tradePrice2]" placeholder="请输入商业价格2" style="width: 100%"/>
        </p-form-item>
        <p-form-item label="税价3" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input-number v-decorator="[ 'taxPrice3', validatorRules.taxPrice3]" placeholder="请输入税价3" style="width: 100%"/>
        </p-form-item>
        <p-form-item label="商业价格3" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input-number v-decorator="[ 'tradePrice3', validatorRules.tradePrice3]" placeholder="请输入商业价格3" style="width: 100%"/>
        </p-form-item>
        <p-form-item label="12位码是否启用" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'twelveBit', validatorRules.twelveBit]" placeholder="请输入12位码是否启用"></p-input>
        </p-form-item>
        <p-form-item label="产品类别" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'productCategory', validatorRules.productCategory]" placeholder="请输入产品类别"></p-input>
        </p-form-item>
        <p-form-item label="拆单方式" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'splitType', validatorRules.splitType]" placeholder="请输入拆单方式"></p-input>
        </p-form-item>
        <p-form-item label="送货方式" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'deliverType', validatorRules.deliverType]" placeholder="请输入送货方式"></p-input>
        </p-form-item>
        <p-form-item label="报价供应商层级" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'quotationLevel', validatorRules.quotationLevel]" placeholder="请输入报价供应商层级"></p-input>
        </p-form-item>
        <p-form-item label="最小包装量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input-number v-decorator="[ 'minPacking', validatorRules.minPacking]" placeholder="请输入最小包装量" style="width: 100%"/>
        </p-form-item>
        <p-form-item label="是否显示价格排名" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'displayList', validatorRules.displayList]" placeholder="请输入是否显示价格排名"></p-input>
        </p-form-item>
        <p-form-item label="是否显示价格公示" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'displayPrice', validatorRules.displayPrice]" placeholder="请输入是否显示价格公示"></p-input>
        </p-form-item>
        <p-form-item label="备货状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'stockStatus', validatorRules.stockStatus]" placeholder="请输入备货状态"></p-input>
        </p-form-item>
        <p-form-item label="备货计划显示周数" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'stockPlan', validatorRules.stockPlan]" placeholder="请输入备货计划显示周数"></p-input>
        </p-form-item>
        <p-form-item label="计算方式" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'formulaModel', validatorRules.formulaModel]" placeholder="请输入计算方式"></p-input>
        </p-form-item>
        <p-form-item label="更新人标识" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'updatetimeBy', validatorRules.updatetimeBy]" placeholder="请输入更新人标识"></p-input>
        </p-form-item>
        <p-form-item label="更新时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'updatetimeTime', validatorRules.updatetimeTime]" placeholder="请输入更新时间"></p-input>
        </p-form-item>
        <p-form-item label="时间标识" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'timestamp', validatorRules.timestamp]" placeholder="请输入时间标识"></p-input>
        </p-form-item>
        <p-form-item label="公司标识" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'companyId', validatorRules.companyId]" placeholder="请输入公司标识"></p-input>
        </p-form-item>
        <p-form-item label="工厂标识" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'factoryId', validatorRules.factoryId]" placeholder="请输入工厂标识"></p-input>
        </p-form-item>
        <p-form-item label="租户标识" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'clientId', validatorRules.clientId]" placeholder="请输入租户标识"></p-input>
        </p-form-item>
        <p-form-item label="替换人员标识" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'substituteId', validatorRules.substituteId]" placeholder="请输入替换人员标识"></p-input>
        </p-form-item>
        <p-form-item label="物料类别码" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'classCode', validatorRules.classCode]" placeholder="请输入物料类别码"></p-input>
        </p-form-item>
        <p-form-item label="供应商编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'supplierCode', validatorRules.supplierCode]" placeholder="请输入供应商编码"></p-input>
        </p-form-item>
        <p-form-item label="创建人" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'createBy', validatorRules.createBy]" placeholder="请输入创建人"></p-input>
        </p-form-item>
        <p-form-item label="物料状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['status', validatorRules.status]" :trigger-change="true" dictCode="mat_status" placeholder="请选择物料状态"/>
        </p-form-item>

      </p-form>
    </p-spin>
    <p-button type="primary" @click="handleOk">确定</p-button>
    <p-button type="primary" @click="handleCancel">取消</p-button>
  </p-drawer>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  
  export default {
    name: "MaterialSrmModal",
    components: { 
      JDictSelectTag,
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title: '操作',
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          code: {rules: [
          ]},
          name: {rules: [
            {required: true, message: '请输入物料名称!'},
          ]},
          weightScope: {rules: [
          ]},
          volumeScope: {rules: [
          ]},
          extendGroup: {rules: [
          ]},
          markAbc: {rules: [
          ]},
          codeOld: {rules: [
          ]},
          minimumElement: {rules: [
          ]},
          batchNumber: {rules: [
          ]},
          minimumReceived: {rules: [
          ]},
          overlapedTolerance: {rules: [
          ]},
          minimumNumber: {rules: [
          ]},
          maxNumber: {rules: [
          ]},
          largestLevel: {rules: [
          ]},
          taxStandard: {rules: [
          ]},
          taxPrice1: {rules: [
          ]},
          tradePrice1: {rules: [
          ]},
          taxPrice2: {rules: [
          ]},
          tradePrice2: {rules: [
          ]},
          taxPrice3: {rules: [
          ]},
          tradePrice3: {rules: [
          ]},
          twelveBit: {rules: [
          ]},
          productCategory: {rules: [
          ]},
          splitType: {rules: [
          ]},
          deliverType: {rules: [
          ]},
          quotationLevel: {rules: [
          ]},
          minPacking: {rules: [
          ]},
          displayList: {rules: [
          ]},
          displayPrice: {rules: [
          ]},
          stockStatus: {rules: [
          ]},
          stockPlan: {rules: [
          ]},
          formulaModel: {rules: [
          ]},
          updatetimeBy: {rules: [
          ]},
          updatetimeTime: {rules: [
          ]},
          timestamp: {rules: [
          ]},
          companyId: {rules: [
          ]},
          factoryId: {rules: [
          ]},
          clientId: {rules: [
          ]},
          substituteId: {rules: [
          ]},
          classCode: {rules: [
          ]},
          supplierCode: {rules: [
          ]},
          createBy: {rules: [
          ]},
          status: {rules: [
          ]},
        },
        url: {
          add: '/basic-business/basicSrm/materialSrm/add',
          edit: '/basic-business/basicSrm/materialSrm/edit',
        }
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },

      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'code','name','weightScope','volumeScope','extendGroup','markAbc','codeOld','minimumElement','batchNumber','minimumReceived','overlapedTolerance','minimumNumber','maxNumber','largestLevel','taxStandard','taxPrice1','tradePrice1','taxPrice2','tradePrice2','taxPrice3','tradePrice3','twelveBit','productCategory','splitType','deliverType','quotationLevel','minPacking','displayList','displayPrice','stockStatus','stockPlan','formulaModel','updatetimeBy','updatetimeTime','timestamp','companyId','factoryId','clientId','substituteId','classCode','supplierCode','createBy','status'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.msg);
                that.$emit('ok');
              }else{
                that.$message.warning(res.msg);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
         
        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'code','name','weightScope','volumeScope','extendGroup','markAbc','codeOld','minimumElement','batchNumber','minimumReceived','overlapedTolerance','minimumNumber','maxNumber','largestLevel','taxStandard','taxPrice1','tradePrice1','taxPrice2','tradePrice2','taxPrice3','tradePrice3','twelveBit','productCategory','splitType','deliverType','quotationLevel','minPacking','displayList','displayPrice','stockStatus','stockPlan','formulaModel','updatetimeBy','updatetimeTime','timestamp','companyId','factoryId','clientId','substituteId','classCode','supplierCode','createBy','status'))
      }
      
    }
  }
</script>

<style lang="less" scoped>
/** Button按钮间距 */
  .ant-btn {
    margin-left: 30px;
    margin-bottom: 30px;
    float: right;
  }
</style>